//
//  SWMainViewController.swift
//  BaseDemo
//
//  Created by linhan on 16/2/17.
//  Copyright © 2016年 me. All rights reserved.
//

import Foundation
import UIKit

class SWRootViewController: SWBaseViewController
{
    private var _mainViewController:UIViewController?{
        didSet{
            if let oldVC = oldValue{
                easyRemoveChildViewController(oldVC)
                //trace("RemoveChildViewController:", oldVC)
            }
            if let newVC = _mainViewController{
                easyAddChildViewController(newVC)
                print("easyAddChildViewController")
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if appearedFirstTime
        {
            appearedFirstTime = false
            
            /**
            //使内容强制转向到目标方向，目标方向可以跟设备方向不同
            let vc = UIViewController()
            self.present(vc, animated: false, completion: nil)
            self.dismiss(animated:false, completion: nil)
             **/
        }
    }
    
    override var navigationController: UINavigationController? {
        return _mainViewController as? UINavigationController
    }
    
    private func setup()
    {
        SWMainApplication.sharedApplication.rootViewController = self
        //let childrenCount:Int = SWAccountManager.sharedManager().account?.children?.count ?? 0
        //if !(SWAccountManager.sharedManager().logined && childrenCount > 0)
        if !(SWAccountManager.sharedManager().logined)
        {
            auth(nil, redirectParam: nil, presentation: false)
        }
        else
        {
            setMainContent()
        }
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(needLogin), name: NotificationName.NeedLogin, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(logout), name: NotificationName.Logout, object: nil)
    }
    
    private var _redirectPage:String?
    private var _redirectParam:[NSObject:AnyObject]?
    func auth(_ redirectPage:String? = nil, redirectParam:[NSObject:AnyObject]? = nil, presentation:Bool = true)
    {
        trace("auth...")
        if presentation
        {
            var presentable:Bool = true
            if let tag = presentedViewController?.view.tag , tag == SW.ViewTag.LoginViewController
            {
                presentable = false
            }
            
            //如果当前显示着，则不再生成显示
            if presentable
            {
                //防止重复调用导致重定向参数被覆盖
                _redirectPage = redirectPage
                _redirectParam = redirectParam
                
                let navigation:SWBaseNavigationController = SWBaseNavigationController(rootViewController: SWLoginViewController())
                navigation.view.tag = SW.ViewTag.LoginViewController
                present(navigation, animated: true, completion: nil)
            }
        }
        else
        {
            let navigationController:SWBaseNavigationController = SWBaseNavigationController(rootViewController: SWLoginViewController())
            _mainViewController = navigationController
        }
    }
    
    func showMainContent()
    {
        if let viewController = presentedViewController , viewController.view.tag == SW.ViewTag.LoginViewController
        {
            viewController.dismiss(animated:true, completion: {
                if let page = self._redirectPage , page != ""{
                    if page == "CheckIn"
                    {
                        //self.navigationController?.pushViewController(SWCheckinViewController(), animated: true)
                    }
                }
            })
            
            
        }
        
        setMainContent()
    }
    
    func setMainContent()
    {
        print("setMainContent")
        let mainVC:SWIndexViewController = SWIndexViewController()
        let viewController:SWBaseNavigationController = SWBaseNavigationController(rootViewController: mainVC)
        
        SWMainApplication.sharedApplication.mainViewController = viewController
        SWMainApplication.sharedApplication.mainViewControllerReady()
        _mainViewController = viewController
    }
    
    //未登陆，需登陆
    @objc private func needLogin()
    {
        auth()
    }
    
    @objc private func logout()
    {
        auth(nil, redirectParam: nil, presentation: false)
    }


}
